package cn.driver.reserve.controller;

import cn.driver.common.pojo.Reserve;
import cn.driver.common.tools.ResponseData;
import cn.driver.reserve.service.ReserveService;
import cn.driver.reserve.tools.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.List;

@Api("预约管理接口")
@RestController
@RequestMapping("/reserve")
public class ReserveController {
    @Autowired
    private ReserveService service;

    @ApiOperation(value = "获取完整预约列表", notes = "只有管理员才能获取完整列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "index", value = "获取页码", required = true),
            @ApiImplicitParam(paramType = "path", name = "resId", value = "要查询的预约ID(模糊查询)")
    })
    @GetMapping({"/admin/{index}/{resId}", "/admin/{index}"})
    public ResponseData<Page> adminGetReserveList(@PathVariable int index, @PathVariable(required = false) String resId) {
        int pageSize = 20;
        return ResponseData.ok(new Page(index, pageSize, service.getReserveCount(), service.getFullReserveList(index, pageSize, resId != null ? resId : "")));
    }

    @ApiOperation(value = "根据教练ID获取预约列表", notes = "当教练登录并查看预约列表时,为其列出预约自己的列表")
    @ApiImplicitParam(paramType = "path", name = "coaId", value = "要查询的教练ID", required = true)
    @GetMapping("/coach/{coaId}")
    public ResponseData<List<Reserve>> coachGetReserveList(@PathVariable int coaId) {
        return ResponseData.ok(service.getReserveListByCoach(coaId));
    }

    @ApiOperation(value = "根据学员ID获取预约", notes = "当学员完成预约后再次进入预约页面时,获取其预约详情并将其自动填入表单中便于修改")
    @ApiImplicitParam(paramType = "path", name = "stuId", value = "要查询的学员ID", required = true)
    @GetMapping("/student/{stuId}")
    public ResponseData<Reserve> studentGetReserve(@PathVariable int stuId) {
        return ResponseData.ok(service.getReserveByStuId(stuId));
    }

    @ApiOperation(value = "添加新的预约", notes = "预约号由添加时自动生成,无需指定,管理员与学员可以添加预约,教练不可以")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "coaId", value = "教练ID", required = true),
            @ApiImplicitParam(paramType = "query", name = "stuId", value = "学生ID", required = true),
            @ApiImplicitParam(paramType = "query", name = "courseId", value = "课程ID", required = true),
            @ApiImplicitParam(paramType = "query", name = "resTime", value = "预约时间", required = true),
            @ApiImplicitParam(paramType = "query", name = "comment", value = "备注")
    })
    @ApiOperationSupport(ignoreParameters = {"resId", "status", "student.*", "coach.*", "course.id", "course.course", "course.state"})
    @PostMapping
    public ResponseData<String> addNewReserve(Reserve reserve) {
        Calendar calendar = Calendar.getInstance();
        String month = calendar.get(Calendar.MONTH) + 1 < 10 ? "0" + (calendar.get(Calendar.MONTH) + 1) : "" + calendar.get(Calendar.MONTH) + 1;
        String resId = "" + calendar.get(Calendar.YEAR) + month + calendar.get(Calendar.DATE);
        System.out.println(resId);
        int num = service.getCountByCreateDate(resId);

        if (num < 9) {
            resId += "00" + (num + 1);
        } else if (num < 99) {
            resId += "0" + (num + 1);
        } else if (num < 999) {
            resId += "" + (num + 1);
        } else {
            return ResponseData.error("今日预约数过多,请择日再来");
        }
        reserve.setResId(resId);
        return service.insertNewReserve(reserve) ? ResponseData.ok("添加成功!") : ResponseData.error("添加失败!");
    }

    @ApiOperation(value = "修改预约", notes = "管理员可以自由修改预约,学员在添加预约后可以在同一页面对自己的预约进行修改,教练不能修改预约")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "resId", value = "预约ID", required = true),
            @ApiImplicitParam(paramType = "query", name = "coaId", value = "教练ID"),
            @ApiImplicitParam(paramType = "query", name = "stuId", value = "学生ID"),
            @ApiImplicitParam(paramType = "query", name = "courseId", value = "课程ID"),
            @ApiImplicitParam(paramType = "query", name = "resTime", value = "预约时间"),
            @ApiImplicitParam(paramType = "query", name = "comment", value = "备注")
    })
    @ApiOperationSupport(ignoreParameters = {"status", "student.*", "coach.*", "course.id", "course.course", "course.state"})
    @PutMapping
    public ResponseData<String> updateReserve(Reserve reserve) {
        return service.updateReserve(reserve) ? ResponseData.ok("修改成功!") : ResponseData.error("修改失败!");
    }

    @ApiOperation(value = "删除预约", notes = "当学员或管理员撤销预约或教练拒绝预约时,将对应记录删除")
    @ApiImplicitParam(paramType = "path", name = "resId", value = "要删除的预约ID", required = true)
    @DeleteMapping("/{resId}")
    public ResponseData<String> deleteReserve(@PathVariable String resId) {
        return service.deleteReserve(resId) ? ResponseData.ok("删除成功!") : ResponseData.error("删除失败!");
    }

    @ApiOperation(value = "同意预约", notes = "教练和管理员可以同意某位学员的预约,只有被同意的预约才会生效(待实现:当预约被同意或拒绝后向学员发送消息提醒)")
    @ApiImplicitParam(paramType = "path", name = "resId", value = "要同意的预约ID", required = true)
    @PutMapping("/agree/{resId}")
    public ResponseData<String> agreeReserve(@PathVariable String resId) {
        return service.changeResStatus(resId) ? ResponseData.ok("预约已被同意!") : ResponseData.error("操作失败!");
    }
}
